﻿#region Using Directives

    using Microsoft.Reporting.WebForms;

    using System;
    using System.Collections.Generic;
    using System.Configuration;
    using System.Linq;
    using System.Net;
    using System.Security.Principal;
    using System.Web;

#endregion

namespace MarketPulse.Web.Helpers
{

    /// <summary>
    /// Class used to provide credentials for (Reporting Services) report server
    /// </summary>
    public class ReportServerCredentials : IReportServerCredentials
    {
        #region Member Variables

            /// <summary>Property that stores the name of the class.</summary>
            protected static readonly Type mClass = System.Reflection.MethodBase.GetCurrentMethod().DeclaringType;

            /// <summary>Property that stores the log4net logger for this class.</summary>
            protected static readonly log4net.ILog mLog = log4net.LogManager.GetLogger(mClass);

            #endregion

        #region Constructor(s)

            /// <summary>
            /// Default constructor for this class
            /// </summary>
            public ReportServerCredentials()
            {
                SetReportCredentials();
            }

        #endregion

        #region Methods

            /// <summary>
            /// Method used to determine whether Forms based authentiucation is being used
            /// </summary>
            /// <param name="authCookie">The authentication cookie being used</param>
            /// <param name="username">The username being used</param>
            /// <param name="password">The password being used</param>
            /// <param name="authority">The authority being used</param>
            /// <returns>A boolean value used to determine whether forms based authentication is being used</returns>
            public bool GetFormsCredentials(out Cookie authCookie, out string username, out string password, out string authority)
            {
                authCookie = null;
                username = null;
                password = null;
                authority = null;

                // Not using form credentials
                return false;
            }

            #region Local Methods

                /// <summary>
                /// Method used to set the credentials based properties (domain, username, password) from the web.config file
                /// </summary>
                private void SetReportCredentials()
                {
                    try
                    {
                        // Set the domain name
                        if (string.IsNullOrEmpty(DomainName))
                        {
                            DomainName = ConfigurationManager.AppSettings["ReportServerDomain"];

                            if (string.IsNullOrEmpty(DomainName))
                            {
                                throw new Exception("Missing domain from web.config file");
                            }
                        }

                        // Set the username
                        if (string.IsNullOrEmpty(Username))
                        {
                            Username = ConfigurationManager.AppSettings["ReportServerUsername"];

                            if (string.IsNullOrEmpty(Username))
                            {
                                throw new Exception("Missing report server username from web.config file");
                            }
                        }

                        // Set the password
                        if (string.IsNullOrEmpty(Password))
                        {
                            Password = ConfigurationManager.AppSettings["ReportServerPassword"];

                            if (string.IsNullOrEmpty(Password))
                            {
                                throw new Exception("Missing password from web.config file");
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        mLog.Error("Exception thrown on attempt to set the credentials while porcessing 'SetReportCredentials' method of the 'ReportServerCredentials' class", ex);
                        throw ex;
                    }
                }

            #endregion

        #endregion

        #region Properties

            /// <summary>
            /// Property used to forward the Windows/Network credentials to the report server
            /// </summary>
            public WindowsIdentity ImpersonationUser
            {
                get
                {
                    // Not using the windows credentials
                    return null;
                }
            }

            /// <summary>
            /// Property used to forward on the credentials from a web.config file
            /// </summary>
            public ICredentials NetworkCredentials
            {
                get
                {
                    SetReportCredentials();
                    return new NetworkCredential(Username, Password, DomainName);
                }
            }

            /// <summary>The DomainName used as part of the credentials</summary>
            public string DomainName { get; set; }

            /// <summary>The Username used as part of the credentials</summary>
            public string Username { get; set; }

            /// <summary>The Password used as part of the credentials</summary>
            public string Password { get; set; }

        #endregion

    }
}